package com.lightboat.common.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.lightboat.common.core.domain.bo.ColumnBo;
import com.lightboat.common.core.domain.bo.OrderItemBo;
import com.lightboat.common.function.SFunction;
import com.lightboat.common.utils.sql.SqlUtil;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Getter
public class ChainOrderBy<T> {


    private final List<OrderItemBo> items = Lists.newArrayList();

    public ChainOrderBy<T> orderBy(SFunction<T, ?> func, boolean isAsc) {
        String column = LambdaUtils.getColumn(func).getColumnSelect();
        if (isAsc) {
            items.add(OrderItemBo.asc(column));
        } else {
            items.add(OrderItemBo.desc(column));
        }
        return this;
    }

    /**
     * 添加排序
     * @param func
     * @return
     */
    @SafeVarargs
    public final ChainOrderBy<T> orderByDesc(SFunction<T, ?>... func) {
        String[] columns = Arrays.stream(func).map(LambdaUtils::getColumn).map(ColumnBo::getColumnSelect).toArray(String[]::new);
        items.addAll(OrderItemBo.descs(columns));
        return this;
    }

    public final ChainOrderBy<T> orderByDesc(boolean b,String s) {
       if (b){
           items.addAll(OrderItemBo.descs(s));
       }
        return this;
    }

    public final ChainOrderBy<T> orderByDesc(String s) {
        items.addAll(OrderItemBo.descs(s));
        return this;
    }

    /**
     * 添加排序
     * @param func
     * @return
     */
    @SafeVarargs
    public final ChainOrderBy<T> orderByAsc(SFunction<T, ?>... func) {
        String[] columns = Arrays.stream(func).map(LambdaUtils::getColumn).map(ColumnBo::getColumnSelect).toArray(String[]::new);
        items.addAll(OrderItemBo.ascs(columns));
        return this;
    }

    public final ChainOrderBy<T> orderByAsc(String s) {
        items.addAll(OrderItemBo.ascs(s));
        return this;
    }

    public final ChainOrderBy<T> orderByAsc(boolean b,String s) {
        if (b){
            items.addAll(OrderItemBo.ascs(s));
        }
        return this;
    }

    public void apply() {
        Page<Object> localPage = Optional.ofNullable(PageHelper.getLocalPage()).orElseGet(Page::new);
        String orderBy = localPage.getOrderBy();
        List<OrderItemBo> finalItems = Lists.newArrayList(items);
        if (ObjUtil.isNotEmpty(orderBy)) {
            List<SortParamConverter.SortField> sortFields = SortParamConverter.convertSortParams(orderBy);
            finalItems.addAll(sortFields.stream()
                    .map(sortField -> "asc".equals(sortField.order()) ? OrderItemBo.asc(sortField.field()) : OrderItemBo.desc(sortField.field()))
                    .toList());
        }
        List<OrderItemBo> distinct = CollUtil.distinct(finalItems, OrderItemBo::getColumn, true);
        String orderByStr = distinct.stream()
                .map(m -> m.getColumn() + " " + (m.isAsc() ? "asc" : "desc"))
                .collect(Collectors.joining(","));
        SqlUtil.escapeOrderBySql(orderByStr);
        PageHelper.orderBy(orderByStr);
    }
}